<template>
    <div class="prj-edit-document-view">

      <el-button type="primary" icon="el-icon-edit" size="mini" @click="addDoc">新增文档</el-button>

      <el-table
        size="medium"
        :data="documents.list"
        align="center"
        border
        style="width: 100%">
        <el-table-column
          prop="file_name"
          label="文档名称"
          width="200">
        </el-table-column>
        <el-table-column
          prop="file_type"
          label="文档类型"
          width="100">
        </el-table-column>
        <el-table-column
          label="申报所需文件"
          width="150">
          <template slot-scope="scope">
            <span>{{scope.row.need?'是':'否'}}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="task_name"
          label="提交任务"
          width="200">
        </el-table-column>
        <el-table-column
          prop="name"
          label="操作"
        >
          <template slot-scope="scope">
            <el-button size="medium" type="text" icon="el-icon-edit" @click="editClick(scope.row,scope.$index)">
              编辑
            </el-button>
            <el-button class="text-btn-red" size="medium" type="text" icon="el-icon-delete" @click="deleteClick(scope.$index)">
              删除
            </el-button>

          </template>

        </el-table-column>
      </el-table>

      <document-edit ref="documentEdit" :datas="documentData" :plans="plans" @succeed="documentSucceed"></document-edit>

    </div>
</template>

<script>
  import documentEdit from "./documentEdit";
  import router from "../../../../../router";
    export default {
        name: "documentView",
        components:{documentEdit},
        props:{
            documents:{
                type:Object,
                default:function () {
                    return{
                        list:[],
                        id:0,
                    }
                }
            },
            plans:{
                type:Array,
                default:function () {
                    return[]
                }
            }
        },
        data:function () {
            return{
                documentData:{},
                editIndex:-1,
                options: [{
                    id: '1',
                    label: '类型1'
                }, {
                    id: '2',
                    label: '类型2'
                }]
            }
        },
        mounted:function(){
            this.options = this.$store.getters.sysDic('file_type')
        },
        methods:{
            deleteClick:function (index) {



                let doc = this.documents.list[index];

                this.documents.list.splice(index,1)

                this.$emit("delectDoc",doc)
            },
            documentSucceed:function(){
                let data = {...this.documentData}

                if (this.editIndex == -1){
                    data.id = this.documents.id + ""
                    this.documents.id++;
                    this.documents.list.push(data)
                }else{
                    this.$emit("delectDoc",this.documents.list[this.editIndex])
                    this.$set(this.documents.list,this.editIndex,data)
                }
                this.$emit("addDoc",data)

            },
            editClick:function(item,index){
                let data = {...item}
                this.documentData = data;
                this.editIndex = index;

                if (this.$refs.documentEdit){
                    this.$refs.documentEdit.openDialog();
                }
            },
            addDoc:function () {
                this.documentData = {
                    id:"0",
                    file_name:'',
                    file_type:0,
                    type_name:'',
                    need:false,
                    task_id:'0',
                    task_name:'',
                    is_add:true,
                }
                this.editIndex = -1;

                if (this.$refs.documentEdit){
                    this.$refs.documentEdit.openDialog();
                }
            },
        }
    }
</script>

<style lang="scss" scoped>
  @import "../../../../../assets/scss/views/projectManagement/list/documentView.scss";
</style>
